package tvs;

import java.util.ArrayList;
import java.util.List;

public class GameList {

	private List<Game> gameList;
	
	public GameList()
	{
		gameList = new ArrayList<Game>();
		//GameList.addGame(new Game("http://localhost:8080", "Shivesh"));
		
	}
	
	public boolean newGame(String gamename, String ip, String username, String settings, String id)
	{
		Game g = new Game(gamename, ip, username, settings, id);
		addGame(g);
		return true;
	}
	
	public boolean addGame(Game gameObj)
	{
		/*for(Game game : gameList)
		{
			if(game.getName() == gameObj.getName())
			{
				gameObj = new Game(gameObj.getIP(), gameObj.getName()+"(1)", gameObj.getUsername());
				addGame(gameObj);
				break;
			}
		}*/
		System.out.println("Game Added: "+ gameObj.getName());
		return gameList.add(gameObj);
	}
	
	public void removeGame(Game gameObj)
	{
		removeGame(gameObj.getId());
	}
	
	public void removeGame(String gameid)
	{
		for(Game game : gameList)
		{
			if(game.getId().equals(gameid))
			{
				gameList.remove(gameList.indexOf(game));
				break;
			}
		}
	}
	
	public Game getGame(String gameid)
	{
		System.out.println("Ammount of games: "+gameList.size());
		for(Game game : gameList)
		{
			if(game.getId().equals(gameid))
			{
				return game;
			}
		}
		return null;
	}
	
	public List<Game> getList()
	{
		return gameList;
	}
	
	public void setGame(Game g)
	{
		this.removeGame(g);
		this.addGame(g);
	}
	
	public void addToGameBanList(String gameid, String playername)
	{
		Game g = this.getGame(gameid);
		g.addToBanList(playername);
		this.setGame(g);
	}
	
	public Game addPlayerToGame(String gameid, String playername)
	{
		Game g = this.getGame(gameid);
		boolean isAllowed = true;
		for(String s : g.getBanList())
		{
			if(s.equals(playername))
			{
				isAllowed=false;
			}
		}
		if(isAllowed)
		{
			g.getPlayerList().add(playername);
			//PlayerList pl= g.getPlayerList();
			//List<String> pl = g.getListOfPlayers();
			//pl.addPlayer(new Player(playername));
			//pl.add(playername);
			//g.setPlayerList(pl);
			//this.setGame(g);
		}
		return g;
	}
	
	public Game removePlayerFromGame(String gameid, String playername)
	{
		Game g = this.getGame(gameid);
		//PlayerList pl= g.getPlayerList();
		List<String> pl = g.getPlayerList();
		//pl.removePlayer(new Player(playername));
		pl.remove(pl.indexOf(playername));
		g.setPlayerList(pl);
		this.setGame(g);
		return g;
	}
	
	public Game setSettingsToGame(String gameid, String settings)
	{
		Game g = this.getGame(gameid);
		g.setSettings(settings);
		this.setGame(g);
		return g;
	}
}
